from django.db import models
from django.conf import settings
from django.contrib.auth.models import User


# Create your models here.


class Article(models.Model):
    title = models.CharField(max_length=100, help_text='文章标题', verbose_name='标题')
    context = models.TextField(help_text='文章内容', verbose_name='内容')
    auther = models.ForeignKey(User, on_delete=models.CASCADE, help_text='文章作者', verbose_name='作者')
    like_count = models.PositiveIntegerField(default=0)

    class Meta:  # 元数据
        verbose_name = '文章信息'
        verbose_name_plural = verbose_name
        ordering = ('-like_count',)  # 根据点赞数进行降序排列

    def __str__(self):
        return self.title


class Like(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)  # 关联文章表
    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 关联用户表

    def __str__(self):
        return f'{self.user} likes {self.article}'

    class Meta:
        verbose_name = '点赞信息'
        verbose_name_plural = verbose_name
        unique_together = ('article', 'user')  # 设置唯一
